package cn.zhangxiaohang.thread;

import cn.zhangxiaohang.Utils.HttpRequestUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
public class GetArticleThread extends Thread{
    private Integer count;
    private XSSFSheet sheet;
    private final Integer REQUEST_FAIL_COUNT = 5;
    private AtomicInteger atomicInteger;
    private CopyOnWriteArrayList<String> failUrlList;
    public GetArticleThread(Integer count,XSSFSheet sheet,AtomicInteger atomicInteger,
                            CopyOnWriteArrayList<String> failUrlList){
        this.count = count;
        this.sheet = sheet;
        this.atomicInteger = atomicInteger;
        this.failUrlList = failUrlList;
    }
    /*
     * 多线程获取文章的json数据
     *      通过请求url，获取文件的json数据，请求的时候要设置请求超时次数
     *       通过json数据获取标题，发布时间，图片链接，浏览量
     *       将数据写入excel中，这里要上锁
     * */
    @Override
    public void run() {
        //开始的页数
        int startPage = (count-1)*10+1;
        //结束的页数
        int endPage = startPage+9;
        HashMap<String, String> head = new HashMap<>();
        head.put("cookie"," AGL_USER_ID=bb5dd977-0e39-41ce-9d14-0d1b35e2eb0f; Qs_lvt_412488=1661499569; 53gid2=11683506906004; 53gid0=11683506906004; 53gid1=11683506906004; 53revisit=1661499569793; x-device-sid=fcc5b798-328c-4647-ad0f-19f376ea48c8; Hm_lvt_28bcd804745435dbb0c9437c01ae8d55=1661499570,1661509907; Hm_lpvt_28bcd804745435dbb0c9437c01ae8d55=1661509907; Qs_pv_412488=3085557836765973000%2C3616473793542711300%2C29208736831665570%2C2746980204860770300%2C2961785454255096000; visitor_type=old; 53kf_72471351_from_host=www.aquanliang.com; 53kf_72471351_keyword=; uuid_53kf_72471351=15651235543c47370ee5066bcbbbd8c4; 53kf_72471351_land_page=https%253A%252F%252Fwww.aquanliang.com%252Fblog; kf_72471351_land_page_ok=1; 53uvid=1; onliner_zdfq72471351=0\n");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = startPage; i <=endPage; i++) {
            int currentCount = 1;
            String url = "https://www.aquanliang.com/_next/data/TLvkJPJe-UYg2wHEBZ7F8/blog/page/"+i+".json?page="+i;
            String res = null;
            JSONArray articleList = null;
            //请求重试
            while (REQUEST_FAIL_COUNT >= currentCount)
            {
                try {
                    sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                head.put("referer","https://www.aquanliang.com/blog/page/"+i);

                res = HttpRequestUtil.getStringByGetRequest(url,head);
                if(res == null)
                {
                    System.out.println("请求失败，当次请求次数:"+currentCount+"==请求url:"+url);
                    currentCount++;
                    try {
                        sleep(600);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    continue;
                }
                try {
                    Object parse = JSONObject.parse(res);
                    JSONObject pageProps = getJson(parse, "pageProps", JSONObject.class);
                    articleList = getJson(pageProps, "articleList", JSONArray.class);
                }catch (Exception e){
                    //解析json有问题，本次请求取消
                    e.printStackTrace();
                    System.out.println("获取的json数据有问题，json数据为:"+res);
                    System.out.println("当前url:"+url);
                    currentCount++;
                    try {
                        sleep(600);
                    } catch (InterruptedException e1) {
                        e.printStackTrace();
                    }
                    continue;
                }
                if(articleList== null || articleList.size()==0)
                {
                    System.out.println("获取的articleList为空");
                    currentCount++;
                    try {
                        sleep(600);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    continue;
                }
                break;
            }
            if(currentCount> REQUEST_FAIL_COUNT)
            {
                System.out.println("url:"+url+"请求失败，原因可能是请求超时或者articleList不存在");
                failUrlList.add(url);
                continue;
            }
            System.out.println("请求:"+url+"成功");

            synchronized (sheet){
                for (int j = 0; j < articleList.size(); j++) {
                    //将数据写进excel表中
                    //写的时候需要上锁
                    int cellCount = 0;
                    //获取当前sheet行数,然后将数据写进sheet对应的行中
                    int currentSheetRows = sheet.getPhysicalNumberOfRows();
                    XSSFRow sheetRow = sheet.createRow(currentSheetRows);
                    Object data = articleList.get(j);
                    String title = getJson(data, "title", String.class);
                    sheetRow.createCell(cellCount).setCellValue(title);
                    cellCount++;
                    Integer pub_at = getJson(data, "pub_at", Integer.class);
                    Date date = new Date(pub_at * 1000l);
                    String publishTime = simpleDateFormat.format(date);
                    sheetRow.createCell(cellCount).setCellValue(publishTime);
                    cellCount++;
                    String cover_img = getJson(data, "cover_img", String.class);
                    sheetRow.createCell(cellCount).setCellValue(cover_img);
                    cellCount++;
                    Integer pv = getJson(data, "pv", Integer.class);
                    Integer init_pv = getJson(data, "init_pv", Integer.class);
                    Integer count =pv+init_pv;
                    sheetRow.createCell(cellCount).setCellValue(count);
                }
            }

        }
        System.out.println("线程:"+Thread.currentThread().getName()+"运行结束");
        atomicInteger.incrementAndGet();
    }
    public static <T> T getJson(Object object,String key,Class<T> type){
        JSONObject jsonObject = (JSONObject) object;
        return (T)jsonObject.get(key);
    }
}

